package com.mobileClientKernel.android.serviceLayer.httpTransportService.urlconfig;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;


import com.mobileClientKernel.android.commonLayer.JSONUtil;
import com.mobileClientKernel.android.commonLayer.JJLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;


/**
 * Created by gongjian on 2018/4/28.
 */

public class JJTUrlConfigManager {

    private static final String SERVER_HOST_KEY = "SERVER_HOST_URL";
    private static final String SERVER_PATH_KEY = "SERVER_PATH_URL";
    private static final String ENVIRONMENT = "Environment";
    private static final String BASE = "Base";
    private static JJTUrlConfigManager instance = new JJTUrlConfigManager();

    private String environmentName = "Product";

    private JSONObject urlConfigObject;

    private JJTUrlConfigManager() {
        urlConfigObject = new JSONObject();
        try {
            urlConfigObject.put(ENVIRONMENT, new JSONObject());
            urlConfigObject.put(BASE, new JSONObject());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static JJTUrlConfigManager getInstance() {
        return instance;
    }

    public void setEnvironment(String environmentName){
        this.environmentName = environmentName;
    }

    public void setEnvironmentName(String environmentName) {
        this.environmentName = environmentName;
    }

    public String getEnvironmentName() {
        return this.environmentName;
    }

    public void addUrlConfig(JSONObject urlConfigDic) {
        if (JSONUtil.getJSONObject(this.urlConfigObject, ENVIRONMENT).length() <= 0 && JSONUtil.getJSONObject(this.urlConfigObject, BASE).length() <= 0) {
            this.urlConfigObject = urlConfigDic;
            return;
        }

        try {
            JSONObject inputEnvJSONObject = urlConfigDic.getJSONObject(ENVIRONMENT);
            JSONObject inputBaseJSONObject = urlConfigDic.getJSONObject(BASE);

            JSONObject originBaseJSONObject = this.urlConfigObject.getJSONObject(BASE);
            Iterator<String> iterable = inputBaseJSONObject.keys();
            //循环追加 BASE
            while (iterable.hasNext()) {
                String key = iterable.next();
                originBaseJSONObject.put(key, inputBaseJSONObject.getJSONObject(key));
            }

            JSONObject originEnvJSONObject = this.urlConfigObject.getJSONObject(ENVIRONMENT);
            Iterator<String> envIterable = inputEnvJSONObject.keys();
            //循环追加 BASE
            while (envIterable.hasNext()) {
                String key = envIterable.next();
                originEnvJSONObject.put(key, inputEnvJSONObject.getJSONObject(key));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public boolean existForIdentity(String identity) {

        JSONObject baseJSONObject = JSONUtil.getJSONObject(this.urlConfigObject, BASE);
        JSONObject envJSONObject = JSONUtil.getJSONObject(this.urlConfigObject, ENVIRONMENT);

        JSONObject jsonObject = JSONUtil.getJSONObject(baseJSONObject, identity);
        if (jsonObject != null) {
            return true;
        }

        jsonObject = JSONUtil.getJSONObject(envJSONObject, identity);
        if (jsonObject != null) {
            return true;
        }

        return false;
    }

    public String serverUrl(String identity) {
        String url = serverHostWithIdentity(identity) + serverPathWithIdentity(identity);
        JJLog._d("JJTUrlConfigManager", "[serverUrl] = " + url);
        return url;
    }

    public String serverHostWithIdentity(String identity) {
        String serverHost;
        String serviceHost;
        String serviceBaseHost;

        JSONObject envList = JSONUtil.getJSONObject(urlConfigObject, "Environment");
        JSONObject environment = JSONUtil.getJSONObject(envList, environmentName);
        JSONObject serviceObject = JSONUtil.getJSONObject(environment, identity);
        serviceHost = JSONUtil.getString(serviceObject, SERVER_HOST_KEY);
        if (!TextUtils.isEmpty(serviceHost)) {
            return serviceHost;
        }

        JSONObject baseObject = JSONUtil.getJSONObject(urlConfigObject, "Base");
        JSONObject serviceBaseObject = JSONUtil.getJSONObject(baseObject, identity);
        String hostEnvironmentConfigIdentity = JSONUtil.getString(serviceBaseObject, "SERVER_HOST_Environment_Config_Identity");
        if (!TextUtils.isEmpty(hostEnvironmentConfigIdentity)) {
            try {
                JSONObject hostEnvironmentConfigObject = JSONUtil.getJSONObject(baseObject, hostEnvironmentConfigIdentity);
                JSONObject hostEnvironmentConfigEnvironment = JSONUtil.getJSONObject(hostEnvironmentConfigObject, environmentName);
                serviceBaseHost = JSONUtil.getString(hostEnvironmentConfigEnvironment, SERVER_HOST_KEY);
                if (!TextUtils.isEmpty(serviceBaseHost)) {
                    return serviceBaseHost;
                }
            } catch (Exception exception) {

            }
        }

        serviceBaseHost = JSONUtil.getString(serviceBaseObject, SERVER_HOST_KEY);
        if (!TextUtils.isEmpty(serviceBaseHost)) {
            return serviceBaseHost;
        }

        serverHost = JSONUtil.getString(environment, SERVER_HOST_KEY);
        if (!TextUtils.isEmpty(serverHost)) {
            return serverHost;
        }

        return "";
    }

    public String serverPathWithIdentity(String identity) {
        JSONObject envList = JSONUtil.getJSONObject(urlConfigObject, "Environment");
        JSONObject environment = JSONUtil.getJSONObject(envList, environmentName);
        JSONObject serviceObject = JSONUtil.getJSONObject(environment, identity);
        String servicePath = JSONUtil.getString(serviceObject, SERVER_PATH_KEY);
        if (!TextUtils.isEmpty(servicePath)) {
            return servicePath;
        }

        JSONObject baseObject = JSONUtil.getJSONObject(urlConfigObject, "Base");
        JSONObject serviceBaseObject = JSONUtil.getJSONObject(baseObject, identity);
        String serviceBasePath = JSONUtil.getString(serviceBaseObject, SERVER_PATH_KEY);
        if (!TextUtils.isEmpty(serviceBasePath)) {
            return serviceBasePath;
        }

        return "";
    }
}
